using System.Text.Json;
using IMServer.Models;
using IMServer.Services;

namespace IMServer.Endpoints
{
    public static class UserQueryEndpoint
    {
        public static RouteGroupBuilder MapUserQueryApi(this RouteGroupBuilder group)
        {
            group.MapPost("/contacts", async (HttpContext context) =>
            {
                try
                {
                    using var document = await JsonDocument.ParseAsync(context.Request.Body);
                    if (!document.RootElement.TryGetProperty("UserId", out var userIdElement))
                    {
                        return Results.Json(new
                        {
                            Code = "1101",
                            Message = "用户ID不能为空"
                        });
                    }

                    string userId = userIdElement.GetString() ?? string.Empty;
                    if (string.IsNullOrEmpty(userId))
                    {
                        return Results.Json(new
                        {
                            Code = "1101",
                            Message = "用户ID不能为空"
                        });
                    }

                    // 获取用户信息
                    var user = await UserDbService.Instance.GetUserByUserIdAsync(userId);
                    if (user == null)
                    {
                        return Results.Json(new
                        {
                            Code = "1102",
                            Message = "用户不存在"
                        });
                    }

                    // 获取用户的好友和群组列表
                    var friendList = await UserDbService.Instance.GetUserFriendsAsync(userId);
                    var groupList = await UserDbService.Instance.GetUserGroupsAsync(userId);

                    // 转换为简化的用户信息
                    var friendInfos = friendList
                        .Where(f => f.UserId != null)
                        .Select(f => new
                        {
                            f.UserId,
                            f.UserName,
                            f.Avatar
                        });

                    var groupInfos = groupList
                        .Where(g => g.GroupId != null)
                        .Select(g => new
                        {
                            g.GroupId,
                            g.GroupName,
                            g.Avatar,
                            g.Creator
                        });

                    return Results.Json(new
                    {
                        Code = "0000",
                        Message = "获取成功",
                        Data = new
                        {
                            Friends = friendInfos,
                            Groups = groupInfos
                        }
                    });
                }
                catch (Exception ex)
                {
                    return Results.Json(new
                    {
                        Code = "5001",
                        Message = $"查询失败: {ex.Message}"
                    });
                }
            });
            // 根据ID查询用户信息
            group.MapPost("/userinfo", async (HttpContext context) =>
            {
                try
                {
                    var request = await context.Request.ReadFromJsonAsync<User>();
                    if (request?.UserId == null)
                    {
                        return Results.Json(new
                        {
                            Code = "1101",
                            Message = "用户ID不能为空"
                        });
                    }

                    var user = await UserDbService.Instance.GetUserByUserIdAsync(request.UserId);
                    if (user == null)
                    {
                        return Results.Json(new
                        {
                            Code = "1102",
                            Message = "用户不存在"
                        });
                    }

                    return Results.Json(new
                    {
                        Code = "0000",
                        Message = "获取成功",
                        Data = new
                        {
                            user.UserId,
                            user.UserName,
                            user.Avatar
                        }
                    });
                }
                catch (Exception ex)
                {
                    return Results.Json(new
                    {
                        Code = "5001",
                        Message = $"查询失败: {ex.Message}"
                    });
                }
            });
            return group;
        }
    }
}